#ifndef BINARYHEAD_H
#define BINARYHEAD_H

#include<iostream>
#include<vector>

class BinaryHeap{
    public:
    explicit BinaryHeap( int capacity = 100);
    explicit BinaryHeap( const vector<int> & iteams);

    bool isEmpty() const;
    const int & findMin() const;
    void insert( const int &x);
    void insert(int &&x);
    void deleteMin();
    void deleteMin( int &minItem);
    void makeEmpty();

    private:
    int currentSize;
    vector<int> array;
    void buildHeap();
    void percolateDown( int hole);


};

void insert( const int &x){
    if(currentSize == array.size() +1)
        array.resize(array.size() * 2);

    int copy = x;
    int hole = ++currentSize;

    array[0] = std::move(copy);
    for(; x < array[hole/2];hole /=2 )
        array[hole] = std::move(array[hole/2]);
    array[hole] = std::move(array[0]);

}

void deleteMin(){
    if(isEmpty())
        throw UnderflowException{ };
    array[1] = std::move([array[currentSize--]]);

    percolateDown(1);

}

void percolateDown(int hole){
    int child;
    int tmp = std::move(array[hole]);

    for(; hole * 2 <=currentSize; hole = child){
        child = hole * 2;
        if(child != currentSize ** array[child + 1]  < array[child])
            ++child;

        if(array[child] < tmp)
            array[hole] = std::move(array[child]);
        else
            break;
    }
    array[hole] = std::move(tmp);
}
#endif